/**
 * @date：2014-5-29-上午12:00:00
 * @author: James zhang
 * @Description
 * @version: 1.0.0
 */
package com.zx.platform.utils.report;

import java.lang.reflect.Field;

/**
 * @author: James zhang
 * @Description
 * 
 */
public class ReportBeanUtils {

	/**
	 * 服务报表模块:返回bean的数组
	 * 
	 * @param ob
	 * @return
	 * @throws Exception
	 */
	static Object[] convertBean(Object ob) throws Exception {
		Class<? extends Object> c = ob.getClass();
		Field[] fields = c.getDeclaredFields();
		Object[] retOb = new Object[fields.length];
		for (int i = 0; i < fields.length; i++) {
			fields[i].setAccessible(true);
			retOb[i] = fields[i].get(ob);
		}
		return retOb;
	}
	/**
	 * 将数组转换成bean
	 * @param ob
	 * @return
	 * @throws Exception
	 */
	static void convertToBean(Object[] ob ,Object bean) throws Exception {
		Class<? extends Object> c = bean.getClass();
		Field[] fields = c.getDeclaredFields();
		for (int i = 0; i < ob.length; i++) {
			fields[i].setAccessible(true);
			fields[i].set(bean, ob[i]);
		}
	}
	/**
	 * 
	 * @param ob
	 * @param bean
	 * @throws Exception
	 */
	static void convertToBean(Object[] ob ,Object bean,int[] seq) throws Exception {
		Class<? extends Object> c = bean.getClass();
		Field[] fields = c.getDeclaredFields();
		Field field = null;
		for (int i = 0; i < seq.length; i++) {
			field=fields[seq[i]-1];
			field.setAccessible(true);
			field.set(bean, ob[i]);
		}
	}
	/**
	 * 对bean进行排序和筛选
	 * @param ob
	 * @return
	 * @throws Exception
	 */
	static Object[] convertBeanFilterNSeq(Object ob,int[] seq) throws Exception {
		/**
		 * 假设Bean: name,code,address 正常 int[]={1,2,3} 则data[]={ name,code,address}
		 * 如果int[3,2]  则data[]={address,code}
		 * 而且不知道bean的具体类型,
		 */
		Class<? extends Object> c = ob.getClass();
		Field[] fields = c.getDeclaredFields();
		Field field = null;
		Object[] retOb = new Object[seq.length];
		for(int i=0;i<seq.length;i++ ){
			field=fields[seq[i]-1];
			field.setAccessible(true);
			retOb[i]=field.get(ob);
		}
		return retOb;
	}

	/**
	 * 根据变量名去字段
	 * @param ob
	 * @param feildName
	 * @return
	 * @throws Exception
	 */
	static Object[] convertBeanByFieldName(Object ob, String[] feildName) throws Exception {
		/**
		 * 假设Bean: name,code,address 正常 int[]={1,2,3} 则data[]={ name,code,address}
		 * 如果int[3,2]  则data[]={address,code}
		 * 而且不知道bean的具体类型,
		 */
		Class<? extends Object> c = ob.getClass();
		Field field = null;
		Object[] retOb = new Object[feildName.length];
		for(int i=0;i<feildName.length;i++ ){
			field=c.getDeclaredField(feildName[i]);
			field.setAccessible(true);
			retOb[i]=field.get(ob);
		}
		return retOb;
	}
	/**
	 * 服务报表模块:返回bean长度
	 * 
	 * @param ob
	 * @return
	 * @throws Exception
	 */
	static int getBeanLenh(Object ob) throws Exception {
		Class<? extends Object> c = ob.getClass();
		Field[] fields = c.getDeclaredFields();
		return fields.length;
	}
}